﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Direct Server Shell</title>
    <% Html.RenderPartial("PageIncludes"); %>
    <link href="/core/theme/shell/desktop.css" rel="Stylesheet" media="all" />
    <script type="text/javascript">
        $(function ()
        {
            var Shell = {};
            Shell.init = function ()
            {
                this._pvt = {};
                this._pvt.taskbar = $("#shell-task-bar");
                this._pvt.frames = $("#shell-frames");
                this._pvt.menu = $("#shell-menu");
            };

            Shell.openFrameTab = function (caption, url, id)
            {
                if (!id)
                    id = "tab" + utils.guid();

                //existe um frame com este id?
                var existing = Shell._pvt.frames.find("#" + id);
                if (existing.length)
                {
                    //selecionar o frame existente
                    var frameBtn = Shell._pvt.taskbar.find("[data-frame-ref-id=" + id + "]");
                    if (frameBtn.length)
                    {
                        frameBtn.click();
                        return;
                    }
                }

                //criar a tab e o frame
                var tab = $("<div/>")
                    .addClass("task-window")
                    .attr('id', id);
                tab.appendTo("body");
                var frame = $("<iframe/>")
                    .attr("src", url)
                    .attr("id", id + "_frame")
                    .appendTo(tab);
                this._pvt.frames.children().addClass("ui-helper-hidden");
                this._pvt.frames.append(tab);

                //button
                var button = $("<span/>").addClass("task-item ui-state-default ui-state-active");
                button.attr('data-frame-ref-id', id);
                $("<span/>").addClass("app-icon ui-icon ui-icon-folder-collapsed").appendTo(button);
                $("<span/>").addClass("caption").text(caption).appendTo(button);
                $("<span/>").addClass("close").append("<span class='ui-icon ui-icon-close'/>").appendTo(button);

                this._pvt.taskbar.children().removeClass("ui-state-active");
                this._pvt.taskbar.append(button);
            };

            Shell.init();

            $(".task-item, .start-button")
                .live("mouseenter", function (e)
                {
                    $(this).addClass("ui-state-hover");
                })
                .live("mouseleave", function (e)
                {
                    $(this).removeClass("ui-state-hover");
                });

            $(".task-item").live("click", function ()
            {
                var _this = $(this);
                if (_this.attr("closed") == "1")
                    return;
                if (!_this.hasClass("ui-state-active"))
                {
                    Shell._pvt.frames.children().addClass("ui-helper-hidden");
                    Shell._pvt.taskbar.children().removeClass("ui-state-active");
                    _this.addClass("ui-state-active");
                    Shell._pvt.frames.find("#" + _this.attr("data-frame-ref-id")).removeClass("ui-helper-hidden");
                }
            });

            $(".task-item .close").live("click", function (e)
            {
                var button = $(this).parent(".task-item");
                if (confirm("Deseja fechar este aplicativo?"))
                {
                    button.attr("closed", "1");
                    var isActive = button.hasClass("ui-state-active");
                    Shell._pvt.frames.find("#" + button.attr("data-frame-ref-id")).remove();
                    button.remove();
                    if (isActive)
                    {
                        Shell._pvt.taskbar.children().first().click();
                    }
                }
            });

            $(".start-button").click(function ()
            {
                var menu = Shell._pvt.menu;
                var menuIsVisible = menu.is(":visible");
                if (menuIsVisible)
                    menu.fadeOut('fast');
                else
                    menu.show();
            });

            $("#shell-menu a")
                .live("mouseenter", function (e)
                {
                    $(this).addClass("ui-state-active");
                })
                .live("mouseleave", function (e)
                {
                    $(this).removeClass("ui-state-active");
                })
                .live("click", function (e)
                {
                    e.preventDefault();
                    var _this = $(this);
                    if (_this.attr("id") == "logout-link")
                    {
                        if (confirm("Fazer logout do sistema?"))
                            window.location = "/r/logout";
                        return;
                    }
                    var behavior = (_this.attr("data-behavior") || "").toLowerCase();
                    var href = _this.attr("href");
                    if (behavior == "opendialog")
                    {
                        $.openDialog(href);
                    }
                    else if (behavior == "default")
                    {
                        var frameid = _this.attr("data-frame-id");
                        if (!frameid || !frameid.length)
                        {
                            frameid = "app_frame_" + utils.guid();
                            _this.attr("data-frame-id", frameid);
                        }
                        Shell.openFrameTab(_this.text(), href, frameid);
                    }
                    else
                    {
                        window.open(href);
                    }
                    Shell._pvt.menu.hide();
                });

            var menuTimer = 0;

            $("#shell-menu")
                .mouseenter(function ()
                {
                    if (menuTimer)
                        clearTimeout(menuTimer);
                })
                .mouseleave(function ()
                {
                    menuTimer = setTimeout(function ()
                    {
                        Shell._pvt.menu.fadeOut('fast');
                    }, 500);
                });
        });
    </script>
</head>
<body>
    <div id="shell-container" class="shell-content">
        <div id="shell-header" class="ui-widget ui-widget-header">
            <table width="100%" cellpadding="0" cellspacing="0" border="0">
                <tbody>
                    <tr>
                        <td id="shell-accel-bar">
                            <a href="#" class="start-button ui-state-default"></a>
                        </td>
                        <td id="shell-task-bar">
                        </td>
                        <td id="shell-tools-bar">
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div id="shell-frames">
        </div>
        <div id="shell-menu" class="ui-widget ui-widget-content ui-corner-bottom">
            <%
                var items = ((Direct.ApplicationModel.IAppCommand)ViewData["menu_items"]).Items;
                var links = ((Direct.ApplicationModel.IAppCommand)ViewData["tool_links"]).Items;
            %>
            <ul>
                <%
                    if (items.Count==0)
                    {
                %>
                <li>
                    <span>
                        <i>nenhum aplicativo instalado... </i>
                    </span>
                </li>
                <%
                    }
                    else
                    {
                            foreach (var menuItem in items)
                            {
                                var iconClass = menuItem.Behavior == LinkBehavior.Default ? "ui-icon-folder-collapsed"
                                    : menuItem.Behavior == LinkBehavior.OpenOnNewDialog ? "ui-icon-newwin"
                                    : "ui-icon-extlink";
                %>
                <li>
                    <a href="<%=menuItem.Url%>" class="ui-corner-all" data-behavior="<%=menuItem.Behavior%>" title="<%=menuItem.Hint%>" data-menu-id="<%=menuItem.Id%>">
                        <span class="ui-icon <%= iconClass %>">
                        </span>
                        <span>
                            <%= menuItem.Caption %>
                        </span>
                    </a>
                </li>
                <%
                }
                %>
                <!--li class="separator">
                    <span class="ui-state-default">
                    </span>
                </li-->
                <% }%>
            </ul>
            <ul>
                <li class="separator">
                    <span class="ui-state-default">
                    </span>
                </li>
                
                <li>
                    <a href="/About/Dialog" class="ui-corner-all" data-behavior="OpenDialog">
                        <span class="ui-icon ui-icon-info">
                        </span>
                        <span>
                            Sobre...
                        </span>
                    </a>
                </li>
                <li>
                    <a href="#" class="ui-corner-all" id="logout-link">
                        <span class="ui-icon ui-icon-close">
                        </span>
                        <span>
                            Sair
                        </span>
                    </a>
                </li>
            </ul>
        </div>
    </div>
</body>
</html>
